package gamer.servlet;

import gamer.Bean.User;
import gamer.data.DatabaseOpera;
import jakarta.servlet.http.HttpServlet;

import java.util.HashMap;

public class GameLogin extends HttpServlet {
    HashMap<String, String> u;

    @Override
    protected void service(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse resp) throws jakarta.servlet.ServletException, java.io.IOException {
        User user = (User) req.getSession().getAttribute("user");
        if (user == null)
            user = new User();
        String user_id = req.getParameter("user_id");
        String pwd = req.getParameter("pwd");
        if (user_id == null || pwd == null)
            req.getRequestDispatcher("index.jsp").forward(req, resp);
        else {
            DatabaseOpera dbs = new DatabaseOpera();
            u = dbs.mysqlQuery("*", "user", "user_id='" + user_id + "'");
            if (u != null) {
                if (u.get("pwd").equals(pwd)) {
                    user.setUser(u);
                    req.getSession().setAttribute("user", user);
                }
                req.getRequestDispatcher("jsp/gamer/index.jsp").forward(req, resp);
            }
        }
    }
}
